package ljye.com.ljye.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import ljye.com.ljye.App;
import ljye.com.ljye.R;
import ljye.com.ljye.activity.Activity_Confirm_Order;
import ljye.com.ljye.adapter.Class_GridViewAdapter;
import ljye.com.ljye.bean.Course_Details;
import ljye.com.ljye.utils.Utils;
import ljye.com.ljye.view.MyCircleImageView;


/**
 * 报名 dialog
 */
public class Sign_Dialog extends Dialog implements View.OnClickListener {

    private RelativeLayout mainLayout;
    private LinearLayout main;
    private MyCircleImageView imgClass;//课程图片
    private TextView className;//课程名称
    private TextView money;//课程金额
    private TextView classTime;//课程时间
    private ImageView delete;//删除
    private GridView gridview1;//课时选择
    private GridView gridview2;//上课时段
    private Button determine;//确定
    private Activity context;
    private ArrayList<String> data1;
    private ArrayList<String> data2;
    private String class_selector;//课时选择
    private String class_time;//上课时段
    private Course_Details details;

    public Sign_Dialog(Activity context, Course_Details details) {
        super(context, R.style.AuthenticationDialog);
        this.context=context;
        this.details=details;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_dialog_layout);
        //初始化界面控件
        initView();
        setdeta();
        mainLayout.setOnClickListener(this);
        main.setOnClickListener(this);
        determine.setOnClickListener(this);
        delete.setOnClickListener(this);
        data1 = new ArrayList<>();
        for (int i = 0; i <= 3; i++) {
            data1.add("1个月课时/6节" + i);
        }
        data2 = new ArrayList<>();
        for (int i = 0; i <= 2; i++) {
            data2.add("周一至周五上课" + i);
        }

        initgridview1();//课时选择
        initgridview2();//上课时段
        class_selector= data1.get(0);//默认课时选择第0个
        class_time= data2.get(0);//上课时段选择第0个
    }

    private void setdeta() {
        if (null!=details){
            Course_Details.DataBean data = details.getData();
            //课程图片
            Picasso.with(context)
                    .load(data.getImage_url())
//                .placeholder(R.drawable.ic_default_image)
                    .fit()//填充全部
                    .into(imgClass);
            //课程名称
            className.setText(data.getName());
            //课程价格
            money.setText(data.getMkt_price());
        }
    }

    private void initgridview1() {
        final Class_GridViewAdapter gridViewAdapter=new Class_GridViewAdapter(context, data1);
        gridview1.setAdapter(gridViewAdapter);
        gridview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                gridview1.setAdapter(gridViewAdapter);
                gridViewAdapter.setSeclection(i);
                class_selector= data1.get(i);
                Utils.showToast(context,class_selector);
            }
        });
    }

    private void initgridview2() {
        final Class_GridViewAdapter gridViewAdapter2=new Class_GridViewAdapter(context, data2);
        gridview2.setAdapter(gridViewAdapter2);
        gridview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                gridview2.setAdapter(gridViewAdapter2);
                gridViewAdapter2.setSeclection(i);
                class_time= data2.get(i);
                Utils.showToast(context,class_time);
            }
        });
    }


    private void initView() {

        main = (LinearLayout) findViewById(R.id.main);
        mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
        imgClass = (MyCircleImageView) findViewById(R.id.img_class);
        className = (TextView) findViewById(R.id.class_name);
        money = (TextView) findViewById(R.id.money);
        classTime = (TextView) findViewById(R.id.class_time);
        delete = (ImageView) findViewById(R.id.delete);
        gridview1 = (GridView) findViewById(R.id.gridview1);
        gridview2 = (GridView) findViewById(R.id.gridview2);
        determine = (Button) findViewById(R.id.determine);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_layout:
                dismiss();
                break;
            case R.id.main:
                break;
            case R.id.delete:
                dismiss();
                break;
            case R.id.determine://报名
                Intent intent =new Intent(context, Activity_Confirm_Order.class);
                context.startActivity(intent);
                dismiss();
                App application = (App) context.getApplication();
                String token = application.getToken();
                if (null!=token){
                    Utils.showToast(context,""+token);
                }else {
                    Utils.showToast(context,""+"请先登录");
                }
//                Utils.showToast(context,class_selector+"---"+class_time);
                break;
            default:
                break;
        }
    }
}
